package com.dragon.www.haier.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.Device;

import java.util.List;

/**
 * Created by daiye on 2016/6/6.
 */
public class DevicePickDialog extends Dialog {
    private Context context;
    private List<Device> mDevices = null;
    private ListView mDevicesListView = null;
    private DevicePickAdapter mDevicePickAdapter;

    public DevicePickDialog(Context context) {
        super(context);
        this.context = context;
    }

    public DevicePickDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_device_pick);
        mDevicesListView = (ListView)findViewById(R.id.device_pick_list);
        mDevicePickAdapter = new DevicePickAdapter(context);
        mDevicesListView.setAdapter(mDevicePickAdapter);
        mDevicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(listener!=null){
                    Device device = mDevices.get(position);
                    listener.addDevice(device);
                    dismiss();
                }
            }
        });
    }

    public void setDevices(List<Device> devices){
        this.mDevices = devices;
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        setCanceledOnTouchOutside(true);
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    public class DevicePickAdapter extends BaseAdapter {
        public class DevicePickViewHolder {
            public TextView tv_device;
        }

        private Context mcontext;

        public DevicePickAdapter(Context context) {
            mcontext = context;
        }

        @Override
        public int getCount() {
            return mDevices.size();
        }

        @Override
        public Object getItem(int position) {
            return mDevices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Device device = mDevices.get(position);
            DevicePickViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new DevicePickViewHolder();
                convertView = LayoutInflater.from(mcontext).inflate(R.layout.device_pick_item, parent, false);
                viewHolder.tv_device = (TextView) convertView.findViewById(R.id.device_tv);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (DevicePickViewHolder) convertView.getTag();
            }
            if( device!= null ) {
                viewHolder.tv_device.setText(device.getDeviceName());
            }

            return convertView;
        }
    }


    private OnSelectListener listener;

    public void setLisntener(OnSelectListener listener) {
        this.listener = listener;
    }

    public static abstract interface OnSelectListener {
        public abstract void addDevice(Device device);
    }
}
